<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\learn;

use app\common\model\learn\BookCategory as BookModel;
use think\facade\Db;

/**
 *
 * Class Article
 * @package app\api\model
 */
class Book extends BookModel {

	//设置题目

	public function setDefault($userId) {

		Db::name('user')->where('user_id', $userId)->update(['book_id' => $this->category_id]);
		$book = Db::name('learn_book_category_user')
			->where('user_id', $userId)
			->where('category_id', $this->category_id)->where('is_delete', 0)->find();
		if (!$book) {
			$insertData['category_id'] = $this->category_id;
			$insertData['user_id'] = $userId;
			$insertData['name'] = $this['name'];
			$insertData['total_num'] = Db::name('learn_book')->where('book_name', $this['name'])->count();
			$insertData['store_id'] = self::$storeId;
			$insertData['create_time'] = time();
			Db::name('learn_book_category_user')->insert($insertData);

			$children = Db::name('learn_book_category')
				->where('parent_id', $this->category_id)->where('is_delete', 0)->select()->toArray();
			foreach ($children as $key => $value) {

				$insertData['parent_id'] = $value['parent_id'];
				$insertData['category_id'] = $value['category_id'];
				$insertData['user_id'] = $userId;
				$insertData['name'] = $value['name'];
				$insertData['total_num'] = Db::name('learn_book')->where('book_name', $this['name'])->where('book_name2', $value['name'])->count();
				$insertData['store_id'] = self::$storeId;
				$insertData['create_time'] = time();
				Db::name('learn_book_category_user')->insert($insertData);

				//新增用户题目 -取消，查看题目才新增
				// $ids = Db::name('learn_book')->where('book_name', $this['name'])->where('book_name2', $value['name'])
				// 	->field('subject_id as article_id,store_id,book_name2')->select()->toArray();

				// $insertDataAll = [];
				// foreach ($ids as $key => $item) {
				// 	$item['sort'] = $key + 1;
				// 	$item['user_id'] = $userId;
				// 	$item['book_id'] = $this->category_id;
				// 	$item['book_name2'] = $item['book_name2'];
				// 	$item['create_time'] = time();
				// 	$insertDataAll[] = $item;
				// }

				// Db::name('learn_book_user')->insertAll($insertDataAll);

			}

		} else {

			// $insertData['total_num'] = Db::name('learn_book')->where('book_name', $this['name'])->count();

			// Db::name('learn_book_category_user')
			// 	->where('user_id', $userId)
			// 	->where('book_id', $this->category_id)
			// 	->update($insertData);
		}
		return true;
	}

}